﻿using System;
using System.Collections.Generic;
using System.Text;


namespace lxwmvc
{
    /// <summary>
    /// 功能:转换类，处理各种类型之间的转换
    /// [09.4.10 lxw]<para />
    /// </summary>
    public static class extensionhelper
    {
        /// <summary>
        /// 功能:转换objValue为int
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="iDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回0</returns>
        public static int toint32(this object objValue, int iDefault = 0, bool throwE = false)
        {
            try
            {
                int t;
                if (Int32.TryParse(objValue.ToString().Split('.')[0], out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return iDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return iDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为uint
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="iDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回0</returns>
        public static uint touint32(this object objValue, uint iDefault = 0, bool throwE = false)
        {
            try
            {
                uint t;
                if (UInt32.TryParse(objValue.ToString().Split('.')[0], out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return iDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return iDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为Decimal
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="dDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回0</returns>
        public static decimal todecimal(this object objValue, decimal dDefault = 0, bool throwE = false)
        {
            try
            {
                decimal t;
                if (decimal.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return dDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return dDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue byte[]为Base64String
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="sDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回空字符串</returns>
        public static string tobase64string(this object objValue, string sDefault = "", bool throwE = false)
        {
            try
            {
                return Convert.ToBase64String((byte[])objValue);
            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return sDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为bool
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="bDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回false</returns>
        public static bool toboolean(this object objValue, bool bDefault = false, bool throwE = false)
        {
            try
            {
                bool t;
                if (bool.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return bDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return bDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为Byte
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="bDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回0</returns>
        public static byte tobyte(this object objValue, byte bDefault = 0, bool throwE = false)
        {
            try
            {
                byte t ;
                if (byte.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return bDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return bDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为Char
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="cDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回 '0'</returns>
        public static char tochar(this object objValue, char cDefault = '0', bool throwE = false)
        {
            try
            {
                char t;
                if (char.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return cDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return cDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为DateTime
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="sDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回 '1900-1-1'的日期</returns>
        public static DateTime todatetime(this object objValue, string sDefault = "1900-1-1", bool throwE = false)
        {
            try
            {
                return Convert.ToDateTime(objValue.ToString());
            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return Convert.ToDateTime(sDefault);
            }
        }

        /// <summary>
        /// 功能:转换objValue为Char
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="dDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回 0</returns>
        public static double todouble(this object objValue, double dDefault = 0, bool throwE = false)
        {
            try
            {
                double t;
                if (double.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return dDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return dDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为Int16
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="sDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回 0</returns>
        public static short toint16(this object objValue, short sDefault = 0, bool throwE = false)
        {
            try
            {
                short t;
                if (short.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return sDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return sDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为uInt16
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="sDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回 0</returns>
        public static ushort touint16(this object objValue, ushort sDefault = 0, bool throwE = false)
        {
            try
            {
                ushort t;
                if (ushort.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return sDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return sDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为Int64
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="lDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回 0</returns>
        public static long toint64(this object objValue, long lDefault = 0, bool throwE = false)
        {
            try
            {
                long t;
                if (long.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return lDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return lDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为UInt64
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="lDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回 0</returns>
        public static ulong touint64(this object objValue, ulong lDefault = 0, bool throwE = false)
        {
            try
            {
                ulong t;
                if (ulong.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return lDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return lDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为sbyte
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="sDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回 0</returns>
        public static sbyte tosbyte(this object objValue, sbyte sDefault = 0, bool throwE = false)
        {
            try
            {
                sbyte t;
                if (sbyte.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return sDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return sDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为Float
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="fDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回 0</returns>
        public static float tosingle(this object objValue, float fDefault = 0, bool throwE = false)
        {
            try
            {
                float t;
                if (float.TryParse(objValue.ToString(), out t))
                    return t;
                else
                {
                    if (throwE) throw new Exception("转换失败:" + objValue.tostr());
                    return fDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return fDefault;
            }
        }

        /// <summary>
        /// 功能:转换objValue为String,主要区别于ToString， ToStr为安装转换，可以用ToStr代替toString,string a = null;string b = a.ToStr();string c = a.ToString();
        /// [11-03-10 09:23  周四 lxw]<para />
        /// [11-03-09 10:10  周三 lxw]<para />
        /// </summary>
        /// <param name="objValue">源对象</param>
        /// <param name="sDefault">默认值</param>
        /// <param name="throwE">是否抛出异常</param>
        /// <returns>如果错误,返回""</returns>
        public static string tostr(this object objValue, string sDefault = "", bool throwE = false)
        {
            try
            {
                return objValue.ToString();
            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return sDefault;
            }
        }

        /// <summary>
        /// 去掉小数点
        /// </summary>
        /// <param name="objValue"></param>
        /// <param name="sDefault"></param>
        /// <param name="throwE"></param>
        /// <returns></returns>
        public static string toshuzi(this object objValue, string sDefault = "", bool throwE = false)
        {
            try
            {
                return objValue.tostr().Split('.')[0];
            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return sDefault;
            }
        }
    }
}
